-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Added code to demonstrate use of new I2C timeout feature #356
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me, I left a minor comment inline.
I think it would additionally be good to just define a timeout in all master examples, so that anyone who starts by copying an example will be safe from lockups. I would think just the timeout (with autoreset) is sufficient, no need to handle the timeout flags I think. As for the timeout, maybe 3ms is fine? Or use the higher value used by SMBus (can't remember the exact value just now)?
|
||
Wire.setWireTimeout(3000, true); //timeout value in uSec, true to reset I2C bus on timeout | ||
wireTimeoutCount = 0; | ||
Wire.clearWireTimeoutFlag(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not put this directly after the Wire.begin()
? Seems more clear? Also, is clearing the timeout flag really needed here? We can rely on it being cleared on startup, I think? Same for initializing the count to 0, why not just do that in the variable declaration?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the comment; I moved the anti-lockup code to just under 'Wire.begin()' as you suggested. I wanted to put the timeout flag in to expose its existence to a new user; otherwise they might never know the timeout flag existed at all or how to query it.
I could initialize the variable in the declaration, but I have learned that assuming a variable has been initialized to zero 'somewhere else' can lead to confusing/incorrect behavior. I like making things obvious and explicit, even at the cost of an extra line or two of code. YMMV ;-).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wanted to put the timeout flag in to expose its existence to a new user; otherwise they might never know the timeout flag existed at all or how to query it.
Yeah, that's fine (and I think this might be a good example for it), I was just suggesting not doing the timeout flag handling in all examples (OTOH, maybe it should be).
I like making things obvious and explicit, even at the cost of an extra line or two of code. YMMV ;-).
It does (on this particular thing, I do like explicit in general, but also concise), but it's ok :-)
What about (not) calling clearWireTimeoutFlag()
on startup? You've now added a comment that seems less than helpful?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch! I removed the call to clearWireTimeoutFlag() as it is explicitly cleared in Wire.setWireTimeout()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM now!
I'm not sure where the put the reminder about needed a macro in the Wire library so sketches and libraries can detect the existence of the timeout API functions like setWireTimeout() but a macro needs to exist so that code can tell if the timeout api functions exist. |
I added the macro that @bperrybap suggested in #362. Would it be good to explicitly check for this macro in these examples too? One one hand it is pointless, since these examples are shipped with a core that has the feature, but on the other hand it does result in more portable sketches and maybe raises awareness that not all cores have this timeout feature. |
I think clear documentation is the most important thing. BTW, just noticed that end() is not listed on the Wire reference page.... oops... For these examples, I think it would be useful to at least have a brief comment in the code that mentions that the timeout API functions are not available in all platforms or in older IDE version and perhaps even show an example of using the conditional inside the comment . |
@paynterf, I've taken the liberty to add a commit to your PR that checks for the I thought about adding the Adding such a call might be useful to let people realize that there is a timeout feature, but I wonder if it is really worth the extra complexity, especially if we want to enable timeouts by default in the future anyway. @bperrybap, @greyltc, @cmaglie, @facchinm, any thoughts on this? |
|
@matthijskooijman Good question on if the code should set the timeout. At most maybe a mention of the timeout capability in the comments? |
Revised to demonstrate how to use the new I2C bus timeout feature